package com.yang.chatMultiModel.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.Media;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
public class MultiModelController {

    @Resource
    private ChatClient chatClient;
    @PostMapping("/ai/multiChatTest")
    public Object multiChatTest() throws IOException {
        byte[] imageData = new ClassPathResource("/multimodal.test.png").getContentAsByteArray();

        var userMessage = new UserMessage("请解释在图片中有什么?",
                List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData)));

        ChatResponse response = chatClient.call(
                new Prompt(
                        List.of(userMessage),
                        OpenAiChatOptions
                                .builder()
                                .withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue())
                                .build()
                )
        );
        return response.getResult().getOutput();
    }

    /**
     * 多模态对话
     * @param msg 文本信息
     * @param imageUrl 在线图片地址信息
     */
    @PostMapping("/ai/multiChat")
    public Object multiChat(String msg,String imageUrl){
        // 一张电脑主板背部接口的图片地址 https://img-blog.csdnimg.cn/direct/761932564968468690c3d52c889f329a.png
        UserMessage userMessage = new UserMessage(msg, List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageUrl)));
        ChatResponse response = chatClient.call(
                new Prompt(
                        List.of(userMessage),
                        OpenAiChatOptions
                                .builder()
                                .withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue())
                                .build()
                )
        );
        return response.getResult().getOutput();
    }

    /**
     * 多模态批量图片上传对话
     * @param msg 文本信息
     * @param imageFiles 图片内容 图片过大提示错误
     * @return
     */
    @PostMapping("/ai/multiChat2")
    public Object multiChatWithUploadImage(String msg, List<MultipartFile> imageFiles) {
        List<Media> mediaList = new ArrayList<>();

        for (MultipartFile file : imageFiles) {
            try {
                byte[] imageData = file.getBytes();
                MediaType mediaType = MediaType.valueOf(Objects.requireNonNull(file.getContentType()));
                mediaList.add(new Media(mediaType, imageData));
            } catch (IOException e) {
                // 处理读取图片文件时的异常
                e.printStackTrace();
            }
        }

        UserMessage userMessage = new UserMessage(msg, mediaList);

        ChatResponse response = chatClient.call(
                new Prompt(
                        List.of(userMessage),
                        OpenAiChatOptions
                                .builder()
                                .withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue())
                                .build()
                )
        );

        return response.getResult().getOutput();
    }
}
